import { Notification } from 'element-ui';
import { CANCEL_HTTP_STATUS } from '@/constants';

/**
 * 错误处理
 * @params error ResponseError
 * @params showErrorNotification boolean 是否弹出错误通知
 */
const errorHandler = ({ error, showErrorNotification = true } = {}) => {
  const { status, url, message = '未知错误' } = error;
  const errorText = `error - status:${status}, message:${message}, url:${url}`;
  if (status === CANCEL_HTTP_STATUS || !showErrorNotification) {
    console.info(errorText);
    return;
  }
  console.error(errorText);
  Notification({
    title: '提示',
    message,
    type: 'error',
  });
};

export default errorHandler;
